<?php

/*****************************************************************************
 * 
 *  Copyright 2009 Sylvain Dudoit (contact : sylvain.dudoit at gmail.com)
 *  
 *  This file is part of Poupitchi.
 *
 *  Poupitchi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Poupitchi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Poupitchi.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/


/**    
 * Add a new photo
 * 
 * @param $photo
 * @return unknown_type
 */
function addPhoto(&$photo) {
    global $P_DATA;
    global $P_USER;
    if ($photo['error'] == 0) {
    		$photo['login'] = $P_USER;
        if (substr($photo['type'], 0, 5) == "image") {
            $file = $P_DATA."photos/";
        		while (file_exists($file)) {
	            $name = time().strrchr(strtolower($photo['name']), '.');
	            $file = $P_DATA."photos/".date(P_FILE_DATE, time()).$photo['login']."-".$name;
         		}
            if (!move_uploaded_file($photo['tmp_name'], $file)) {
            	sError(tr("Can't move file : ").$photo['name']);
            }
        } else {
        	sError(tr("Bad file format : ").$photo['type']." ".tr("for")." ".$photo['name']);
        }
    }        
    if ($photo['error'] == 4) {
        return true;
    }
    return false;
}

/**
 * Remove a photo
 * 
 * @param $file
 * @return unknown_type
 */
function removePhoto($file) {
    global $P_DATA;
    global $P_ADMIN;
    if ($P_ADMIN) {
	    if (checkFilename($file)) {
		    @unlink($P_DATA."/cache/thumbs/".$file);
				if (!unlink($P_DATA."/photos/".$file)) {
		      sError(tr("Can't remove photo file !"));
		    }
	    } else {
		  	sError(tr("Invalid file name : ").$file."!");	  	
		  }
    } else {
    	sError(tr("Action not allowed !"));
    }
}

/**
 * Count the number of photos
 * 
 * @return unknown_type
 */
function countPhotos() {
    return count(getPhotos());
}

/**
 * Get a photos list
 * 
 * @param $number
 * @param $start
 * @return unknown_type
 */
function getPhotos($number=-1, $start=0) {
    global $P_DATA;
    $filenames = getDirList($P_DATA."/photos/", $number, $start);
    $photos = array();
    foreach($filenames as $filename) {
        $file = $P_DATA."photos/".$filename;
        $photo = array();
        $photo['file'] = $filename;
        $photos[] = $photo;
    }
    return $photos;
}
  

/**
 * Get a number of photos randomly 
 * @param $number
 * @return an array of photos
 */
function getPhotosLook($number) {
    global $P_DATA;
    $photos = array();
    $filenames = getDirList($P_DATA."photos/");
    $nbfilenames = count($filenames);
    $selection = array();
    if ($number > $nbfilenames) {
        $selection = array_keys($filenames);
    } else {
    		$selection = array_rand($filenames, $number);
    }
    foreach ($selection as $item) {
        $photo = array();
        $photo['file'] = $filenames[$item];
        $photos[] = $photo;
    }
    return $photos;
}


/**
 * Clean cache
 * @return unknown_type
 */
function cleanCache() {
    global $P_DATA;
    $filenames = getDirList($P_DATA."cache/thumbs/");
    foreach($filenames as $filename) {
        unlink($P_DATA."/cache/thumbs/".$filename);
    }
}

?>